<?php


namespace app\appa\controller;


use app\appa\model\AddressModel;
use app\appa\validate\AddressValidate;
use app\common\controller\Api;
use think\Request;

class Address extends Api
{
    public function index(Request $request)
    {
        $address = AddressModel::order('id','DESC')
            ->where('user_id',$request->userId)
            ->field('id,name,mobile,area,address,switch')
            ->select();
        if (!$address){
            $this->error('该用户下没有地址');
        }
        $this->success('查询成功',$address);
    }

    public function add(Request $request)
    {
        $data = input('post.');
        //验证数据
        $validate = new AddressValidate();
        $result = $validate->scene('add')->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        $address = AddressModel::where('address',$data['address'])
            ->where('user_id',$request->userId)
            ->find();
        if ($address){
            $this->error('该用户的地址已经存在');
        }
        $switch = '';
        $add = AddressModel::where('user_id',$request->userId)->find();
        if ($add){
            $switch = 0;
        }else{
            $switch = 1;
        }
        $data['switch'] = $switch;
        $data['user_id'] = $request->userId;
        $data['create_time'] = date('Y-m-d H:i:s');
        if (AddressModel::create($data)){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    public function edit(Request $request)
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('id不能为空');
        }
        $add = AddressModel::field('id,name,mobile,area,address,switch')->find($id);
        if (!$add){
            $this->error('查询失败');
        }
        $this->success('查询成功',$add);
    }

    public function update(Request $request)
    {
        $date = input('post.');
        $validate = new AddressValidate();
        $result = $validate->scene('edit')->check($date);
        if (!$result) {
            $this->error($validate->getError());
        }
        $user = AddressModel::where('id',$date['id'])->find();
        if(!$user){
            return error('该地址未拥有');
        }
        $user->mobile = $date['mobile'];
        $user->area = $date['area'];
        $user->name = $date['name'];
        $user->address = $date['address'];
        if ($user->save()) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

    //设置是否为默认地址
    public function switch(Request $request)
    {
        $id = input('id/d');
        $use = AddressModel::where('id',$id)->find();
        if($use['switch'] == 1){
            $use->switch = 0;
        }else{
            $use->switch = 1;
            AddressModel::where('id','<>',$id)->update(['switch' => 0]);
        }
        if ($use->save()){

            $this->success('修改默认地址成功');
        }
        $this->error('失败');

    }

    public function delete(Request $request)
    {
        $id = input('id/d');
        if (empty($id)) {
            $this->error('id不能为空');
        }
        $news = AddressModel::find($id);
        if (!$news) {
            $this->error('地址不存在');
        }

        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}